/**   
* @Title: PicklistController.java 
* @Package com.csis.system.controller 
* @Description: TODO
* @author dailey  
* @date 2012-10-8
* @version V1.0   
*/
package com.csis.system.controller;

import java.io.PrintWriter;
import java.util.Date;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.csis.framework.domain.FormBean;
import com.csis.framework.util.jqgrid.JqGridModel;
import com.csis.framework.util.jqgrid.JqGridUtils;
import com.csis.system.common.WebUtil;
import com.csis.system.domain.Picklist;
import com.csis.system.domain.PicklistFormBean;
import com.csis.system.domain.Product;
import com.csis.system.service.CustomerService;
import com.csis.system.service.PicklistService;
import com.csis.system.service.ProductService;
import com.csis.system.service.UserService;
import com.csis.system.service.WarehouseService;

/**
 * @author dailey
 *
 */
@Controller
@RequestMapping("/system/pick")
public class PicklistController {
	@Resource
	PicklistService picklistService;
	@Resource
	UserService userService;
	@Resource
	ProductService productService;
	@Resource
	WarehouseService warehouseService;
	@Resource
	CustomerService customerService;

	final static String BASE_URL = "/system/manager/io/pick/";

	protected Log log = LogFactory.getLog(getClass());

	@RequestMapping("/toIndex")
	public String index() {
		return BASE_URL + "index";
	}

	@RequestMapping("/toView")
	public String view(Model model, PicklistFormBean formBean) {
		String saleNo = formBean.getBean().getId();
		Picklist bean = picklistService.load(saleNo);
		if (bean.getWareId() != null)
			bean.setWarehouse(warehouseService.load(bean.getWareId()));
		if (bean.getUserId() != null)
			bean.setUser(userService.load(bean.getUserId()));
		if (bean.getProductId() != null)
			bean.setProduct(productService.load(bean.getProductId()));
		if (bean.getCustomerId() != null) {
			bean.setCustomer(customerService.load(bean.getCustomerId()));
		}
		formBean.setBean(bean);
		model.addAttribute("formBean", formBean);
		return BASE_URL + "detailpick";
	}

	@RequestMapping("/toAdd")
	public String add(Model model, HttpSession session) {
		PicklistFormBean formBean = new PicklistFormBean();
		Picklist bean = new Picklist();
		bean.setUser(WebUtil.getSessionUser(session));
		bean.setUserId(WebUtil.getSessionUser(session).getId());
		formBean.setWares(warehouseService.getAllWarehouse());
		formBean.setBean(bean);
		model.addAttribute("formBean", formBean);
		return BASE_URL + "add";
	}

	@RequestMapping("/add")
	public @ResponseBody
	String add(PicklistFormBean formBean, HttpSession session) {
		String result = "0";
		Picklist bean = formBean.getBean();
		bean.setUser(WebUtil.getSessionUser(session));
		bean.setUserId(WebUtil.getSessionUser(session).getId());
		bean.setPickDate(new Date());
		if (bean.getProductId() != null)
			bean.setProduct(productService.load(bean.getProductId()));
		if(bean.getDiscount()==null)
			bean.setDiscount(1.0F);
		Product product = bean.getProduct();
		if (product != null) {
			if (bean.getProductColor() == null)
				bean.setProductColor(product.getProductColor());
			if (bean.getProductSize() == null)
				bean.setProductSize(product.getProductSize());
			if (bean.getProductPrice() == null) {
				bean.setProductPrice(product.getProductPrice3());
			}
		}
		Float price = bean.getProductPrice();
		Float dis = bean.getDiscount();
		Integer count = bean.getProductCount();
		if (price != null) {
			Float total = null;
			int _count = count == null ? 0 : count;
			float _dis = dis == null ? 0 : dis;
			total = price.floatValue() * _count * _dis;
			bean.setTotal(total);
		}
		try {
			picklistService.save(bean);
			result = "1";
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

	@RequestMapping("/list")
	public String list(Model model) {
		FormBean<?> formBean=new FormBean<Picklist>();
		formBean.setIsAll(false);
		model.addAttribute("formBean", formBean);
		return BASE_URL + "listpick";
	}

	@RequestMapping("/listBy")
	public void listBy(JqGridModel model, HttpServletResponse response, HttpSession session) {
		PicklistFormBean formBean = WebUtil.buildConvert(model).convertToSingle(PicklistFormBean.class);
		formBean.setUserId(WebUtil.getSessionUser(session).getId());
		picklistService.list(formBean);
		response.setContentType("text/json");
		response.setCharacterEncoding("UTF-8");
		PrintWriter os = null;
		try {
			os = response.getWriter();
			String content = JqGridUtils.getJSONParser(PicklistFormBean.class).parse(formBean).getContent();
			os.write(content);
			os.flush();
		} catch (Exception e) {
			if (log.isErrorEnabled())
				log.error(e);
			else
				e.printStackTrace();
		} finally {
			if (os != null)
				os.close();
		}
	}
}
